//
//  PlayAudioViewController.m
//  PublicInfo
//
//  Created by enghou on 2018/1/21.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

#import "PlayAudioViewController.h"
#import <AVKit/AVKit.h>
@interface PlayAudioViewController ()<AVAudioPlayerDelegate>

@end

@implementation PlayAudioViewController{
    CAShapeLayer *progressLayer;
    AVAudioPlayer *player;
    BOOL isPlaying;
    CADisplayLink *link;
    NSTimeInterval duration;
    NSTimeInterval timeElapsed;
    UIButton *play;
    UILabel *timeRemaining;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView *tray = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.height - 252,self.view.width, 252)];
    tray.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:tray];
    progressLayer = [CAShapeLayer layer];
    progressLayer.lineWidth = 3;
    progressLayer.strokeColor = [[UIColor defaultNavibackgroundColor]CGColor];
    progressLayer.frame = CGRectMake(tray.width / 2 - 51, tray.height / 2 - 51, 102, 102);
    [tray.layer addSublayer:progressLayer];
    progressLayer.lineCap = kCALineCapRound;
    progressLayer.fillColor = [[UIColor clearColor]CGColor];
    
    play = [UIButton buttonWithType:UIButtonTypeCustom];
    play.frame = CGRectMake(tray.width / 2 - 50, tray.height / 2 - 50, 100, 100);
    [play setImage:[UIImage imageNamed:@"aio_record_stop_nor"] forState:UIControlStateNormal];
    [play addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
    [tray addSubview:play];
    
    timeRemaining = [[UILabel alloc]init];
    timeRemaining.font = [UIFont systemFontOfSize:14];
    timeRemaining.textColor = [UIColor colorWithHexString:@"999999"];
    timeRemaining.frame = CGRectMake(0, 0, self.view.width, 40);
    timeRemaining.textAlignment = NSTextAlignmentCenter;
    [tray addSubview:timeRemaining];
    if (self.url) {
        NSError *error = nil;
        player = [[AVAudioPlayer alloc]initWithContentsOfURL:self.url error:&error];
        player.delegate = self;
        NSInteger minute = [player duration] / 60;
        NSInteger second = (NSInteger)[player duration]  % 60;
        timeRemaining.text = [NSString stringWithFormat:@"%li'%li''",minute,second];
    }
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [link invalidate];
    [self dismissViewControllerAnimated:NO completion:nil];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    link = [CADisplayLink displayLinkWithTarget:self selector:@selector(refreshPath:)];
    if (@available(iOS 10.0, *)) {
        link.preferredFramesPerSecond = 5;
    }else{
        link.frameInterval = 5;
    }
    [link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    duration = [player duration] * 5;
    isPlaying = [player play];
    if (isPlaying) {
        [play setImage:[UIImage imageNamed:@"aio_record_play_nor"] forState:UIControlStateNormal];
    }
}

-(void)refreshPath:(CADisplayLink *)link{
    timeElapsed++;//一秒5次
    if (duration > 0) {
        CGFloat percent = timeElapsed / duration;
        CGMutablePathRef circlePath = CGPathCreateMutable();
        CGPathAddArc(circlePath, nil, 51, 51, 51, -M_PI_2, percent * M_PI * 3 / 2, NO);
        progressLayer.path = circlePath;
        CGPathRelease(circlePath);
        NSInteger elapsedSecond = timeElapsed / 5;
        NSTimeInterval remaining = [player duration] - elapsedSecond;
        NSInteger minute = remaining / 60;
        NSInteger second = (NSInteger)remaining  % 60;
        timeRemaining.text = [NSString stringWithFormat:@"%li'%li''",minute,second];
    }
}

-(void)play:(UIButton *)btn{
    if (isPlaying) {
        [player pause];
        isPlaying = NO;
        [link setPaused:YES];
        [btn setImage:[UIImage imageNamed:@"aio_record_stop_nor"] forState:UIControlStateNormal];
    }else{
        [player play];
        isPlaying = YES;
        [link setPaused:NO];
        [btn setImage:[UIImage imageNamed:@"aio_record_play_nor"] forState:UIControlStateNormal];
    }
}

-(void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
    [link invalidate];
    [self dismissViewControllerAnimated:NO completion:nil];
}

@end
